package org.supper.test.search;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class ElasticSearch {
    @Resource
    private RestHighLevelClient restHighLevelClient;

    public void add() throws IOException {
        IndexRequest indexRequest = new IndexRequest();
        BulkRequest bulkRequest = new BulkRequest();
        indexRequest.index("huqi");
        indexRequest.id(UUID.randomUUID().toString());
        Map<String,String> source = new HashMap<>();
        source.put("name","胡琪");
        source.put("age","26");
        indexRequest.source(source);
        bulkRequest.add(indexRequest);
        restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
    }
    public Object all(String name) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("huqi");
        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("name",name);
        searchSourceBuilder.query(queryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        SearchHit[] hits1 = hits.getHits();
        List<Map<String,Object>> res = new ArrayList<>();
        for(SearchHit s:hits1) {
            res.add(s.getSourceAsMap());
        }
        return res;
    }

    public Object bool(String name,String age) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("huqi");
        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        QueryBuilder query = QueryBuilders.matchQuery("name",name);
        QueryBuilder query1 = QueryBuilders.matchQuery("age",age);
        queryBuilder.must(query);
        queryBuilder.must(query1);
        searchSourceBuilder.query(queryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        SearchHit[] hits1 = hits.getHits();
        List<Map<String,Object>> res = new ArrayList<>();
        for(SearchHit s:hits1) {
            res.add(s.getSourceAsMap());
        }
        return res;
    }

    public Object group(String name,String age) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("huqi");
        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        QueryBuilder query = QueryBuilders.matchQuery("name",name);
        QueryBuilder query1 = QueryBuilders.matchQuery("age",age);
        queryBuilder.must(query);
        queryBuilder.must(query1);
        searchSourceBuilder.query(queryBuilder);
        searchSourceBuilder.aggregation(AggregationBuilders.terms("count1").field("age.keyword"));
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        SearchHits hits = response.getHits();
        SearchHit[] hits1 = hits.getHits();
        List<Map<String,Object>> res = new ArrayList<>();
        for(SearchHit s:hits1) {
            res.add(s.getSourceAsMap());
        }
        return res;
    }
}
